package com.ruoyi.financial.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.financial.bo.SettingCustOddsBo;
import com.ruoyi.financial.domain.FCustOdds;
import com.ruoyi.financial.mapper.FCustOddsMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.financial.mapper.FOddsRuleMapper;
import com.ruoyi.financial.domain.FOddsRule;
import com.ruoyi.financial.service.IFOddsRuleService;

/**
 * 赔付规则Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-06
 */
@Service
public class FOddsRuleServiceImpl implements IFOddsRuleService {
    @Autowired
    private FOddsRuleMapper fOddsRuleMapper;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private FCustOddsMapper fCustOddsMapper;

    /**
     * 查询赔付规则
     *
     * @param id 赔付规则主键
     * @return 赔付规则
     */
    @Override
    public FOddsRule selectFOddsRuleById(Long id) {
        return fOddsRuleMapper.selectFOddsRuleById(id);
    }

    /**
     * 查询赔付规则列表
     *
     * @param fOddsRule 赔付规则
     * @return 赔付规则
     */
    @Override
    public List<FOddsRule> selectFOddsRuleList(FOddsRule fOddsRule) {
        return fOddsRuleMapper.selectFOddsRuleList(fOddsRule);
    }

    /**
     * 新增赔付规则
     *
     * @param fOddsRule 赔付规则
     * @return 结果
     */
    @Override
    public int insertFOddsRule(FOddsRule fOddsRule) {
        fOddsRule.setCreateTime(DateUtils.getNowDate());
        return fOddsRuleMapper.insertFOddsRule(fOddsRule);
    }

    /**
     * 修改赔付规则
     *
     * @param fOddsRule 赔付规则
     * @return 结果
     */
    @Override
    public int updateFOddsRule(FOddsRule fOddsRule) {
        fOddsRule.setUpdateTime(DateUtils.getNowDate());
        return fOddsRuleMapper.updateFOddsRule(fOddsRule);
    }

    /**
     * 批量删除赔付规则
     *
     * @param ids 需要删除的赔付规则主键
     * @return 结果
     */
    @Override
    public int deleteFOddsRuleByIds(Long[] ids) {
        return fOddsRuleMapper.deleteFOddsRuleByIds(ids);
    }

    /**
     * 删除赔付规则信息
     *
     * @param id 赔付规则主键
     * @return 结果
     */
    @Override
    public int deleteFOddsRuleById(Long id) {
        return fOddsRuleMapper.deleteFOddsRuleById(id);
    }

    @Override
    public Map<String, Object> custOddsList(Long bankerId, Long custId,Integer custType) {
        LoginUser user = SecurityUtils.getLoginUser();

        Map<String, Object> data = new HashMap<>();
        Map<String, Object> checkOddsMap = new HashMap<String, Object>();
        Map<String, List<FOddsRule>> oddsListMap = new HashMap<String, List<FOddsRule>>();

        List<SysDictData> list = dictTypeService.selectDictDataByType("play_type");

        FCustOdds fCustOdds = new FCustOdds();
        fCustOdds.setCustId(custId);
        fCustOdds.setBankerId(bankerId);
        fCustOdds.setUserId(user.getUserId());
        List<FCustOdds> checkCustOddsList = fCustOddsMapper.selectFCustOddsList(fCustOdds);

        FOddsRule fOddsRule = new FOddsRule();
        fOddsRule.setDelFlag("0");
        fOddsRule.setCreateBy(user.getUsername());
        fOddsRule.setBankerId(bankerId);
        fOddsRule.setCustType(custType);
        List<FOddsRule> fOddsRules = selectFOddsRuleList(fOddsRule);

        list.stream().forEach(dictData -> {
            List<FOddsRule> typeList = new ArrayList<>();
            for (FOddsRule odds : fOddsRules) {
                if (dictData.getDictValue().equals(odds.getType())) {
                    typeList.add(odds);
                }
            }
            oddsListMap.put(dictData.getDictValue(), typeList);
            for (FCustOdds custOdds : checkCustOddsList) {
                if (dictData.getDictValue().equals(custOdds.getType())) {
                    checkOddsMap.put(dictData.getDictValue(), custOdds.getOddsId());
                }
            }
        });
        data.put("list", oddsListMap);
        data.put("check", checkOddsMap);
        return data;
    }

    @Override
    public void settingCustOdds(SettingCustOddsBo setting) {
        LoginUser user = SecurityUtils.getLoginUser();
        List<FCustOdds> custOddsList = new ArrayList<>();
        Map<String,Long> map = setting.getData();
        Long bankerId = setting.getBankerId();
        Long custId = setting.getCustId();
        map.keySet().stream().forEach(r-> {
            FCustOdds custOdds = new FCustOdds();
            custOdds.setBankerId(bankerId);
            custOdds.setCustId(custId);
            custOdds.setType(r);
            custOdds.setOddsId(map.get(r));
            custOdds.setUserId(user.getUserId());
            custOddsList.add(custOdds);
        });
        fCustOddsMapper.deleteFCustOdds(bankerId,custId);
        fCustOddsMapper.batchInsertFCustOdds(custOddsList);

    }
}
